<template>
    <div class="inputDom" :style="myStyles">
        <input v-if="isNumber == 0" class="myInput" :placeholder="hits" v-model="inputValue" :class="isHit ? 'pd3' : ''"
            :style="leftText != '' ? { paddingRight: '45px' } : {}" :disabled="disabled" @focus="showList = true">
        <input v-if="isNumber == 1" class="myInput" :placeholder="hits" v-model="inputValue" :class="isHit ? 'pd3' : ''"
            :style="leftText != '' ? { paddingRight: '45px' } : {}" :disabled="disabled" @input="formatInput1" ref="input1">
        <input v-if="isNumber == 2" class="myInput" :placeholder="hits" v-model="inputValue" :class="isHit ? 'pd3' : ''"
            :style="leftText != '' ? { paddingRight: '45px' } : {}" :disabled="disabled" @input="formatInput2" ref="input2">
        <div class="leftText">{{ leftText }}</div>
    </div>
</template>
    
<script>
export default {
    name: 'CommonInputKey',
    emits: ['returnData', 'clearData', 'mustCount'],
    components: {
    },
    props: {
        isShowLeftText: {
            default: false,
            type: Boolean
        },
        hit: {
            default: '',
            type: String
        },
        disabled: Boolean,
        leftText: {
            default: '',
            type: String
        },
        myStyles: {
            default: { width: '160px' }
        },
        isMust: {
            default: false,
            type: Boolean
        },
        mustHit: {
            default: '',
            type: String
        },
        isNumber: {
            default: 2,
            type: Number
        },
        insertKey: String,
        default: {
            default: ''
        },
        max: {
            default: 9999999999,
            type: Number
        },
        min: {
            default: 0,
            type: Number
        },
        isFormat: {          //是否金额和最值格式化
            default: true,
            type: Boolean
        }
    },
    data() {
        return {
            inputValue: this.default,
            hits: this.hit,
            isHit: false,
        }
    },
    mounted() {
    },
    methods: {
        inputReturn() {
            let flag = true
            if (this.isMust && !String(this.inputValue)?.trim()) {
                this.hits = this.mustHit
                this.isHit = true
                flag = false
            }
            return flag
        },
        formatInput1(e) {
            // 保存输入前的光标位置
            let selectionStart = this.$refs.input1.selectionStart;
            // 格式化输入值
            let targetValue = e.target.value
            if (Number(targetValue) < this.min && this.isFormat) {
                targetValue = String(this.min);
            }
            if (Number(targetValue.replace(/[^0-9.]/g, '')) > this.max && this.isFormat) {
                targetValue = String(this.max);
            }
            // 赋值格式化后的结果
            if (this.isFormat)
                this.inputValue = targetValue
                    .replace(/[^0-9]/g, '')
                    .replace(/\B(?=(\d{3})+(?!\d))/g, ',')
            if (!this.isFormat)
                this.inputValue = targetValue
                    .replace(/[^0-9]/g, '')
            if (e.inputType === 'deleteContentBackward') {
                this.$nextTick(() => {
                    // 恢复光标位置
                    this.$refs.input1.selectionStart = selectionStart;
                    this.$refs.input1.selectionEnd = selectionStart;
                });
            }
        },
        formatInput2(e) {
            // 保存输入前的光标位置
            let selectionStart = this.$refs.input2.selectionStart;
            // 格式化输入值
            let targetValue = e.target.value
            if (Number(targetValue) < this.min && this.isFormat) {
                targetValue = String(this.min);
            }
            if (Number(targetValue.replace(/[^0-9.]/g, '')) > this.max && this.isFormat) {
                targetValue = String(this.max);
            }
            // 赋值格式化后的结果
            if (this.isFormat)
                this.inputValue = targetValue
                    .replace(/[^0-9.]/g, '')
                    .replace(/^(\d+\.?\d?)(\d{2})\d*$/, '$1$2')
                    .replace(/\B(?=(\d{3})+(?!\d))/g, ',');
            if (!this.isFormat)
                this.inputValue = targetValue
                    .replace(/[^0-9.]/g, '')
            if (e.inputType === 'deleteContentBackward') {
                this.$nextTick(() => {
                    // 恢复光标位置
                    this.$refs.input2.selectionStart = selectionStart;
                    this.$refs.input2.selectionEnd = selectionStart;
                });
            }
        }
    },
    watch: {
        inputValue: {
            handler() {
                this.isHit = false
                let temp = { [this.insertKey]: this.isNumber > 0 ? this.inputValue == '' ? '' : Number(this.inputValue.replace(/[^0-9.]/g, '')) : this.inputValue }
                return this.$emit('returnData', temp)
            }
        },
        default() {
            if (this.inputValue == '' || this.inputValue == null) {
                this.inputValue = this.default
            }
            if (this.insertKey == 'address') {
                this.inputValue = this.default
            }
        },
        hit() {
            this.hits = this.hit
        }
    },
    computed: {
    }

}
</script>
    
<style scoped>
.inputDom {
    display: flex;
    align-items: center;
    position: relative;
}

.myInput {
    outline: none;
    height: 20px;
    border: 1px solid rgb(220, 223, 230);
    padding-left: 8px;
    padding-right: 20px;
    border-radius: 3px;
    font-size: 12px;
    position: relative;
    margin: 1px;
    width: 100%;
}

.myInput:focus {
    border: 1px solid #6698ff;
}

.leftText {
    font-size: 12px;
    color: #999;
    position: absolute;
    right: 12px;
}

.pd3 {
    padding-left: 15px;
    padding-right: 10px;
    color: red;
    border: 1px solid red;
}

.pd3::placeholder {
    color: red;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

input[type="number"] {
    -moz-appearance: textfield;
    appearance: none;
}
</style>